package com.yl.utils.speechToText;

import com.yl.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.nls.client.AccessToken;

import java.io.IOException;

/**
 * 阿里的语音转文字
 *
 * @author : y
 * @date :  2024/7/14 14:46
 **/
public class SpeechToTextUtil {
    private static final Logger log = LoggerFactory.getLogger(SpeechToTextUtil.class);

    private static final String appKey = "eLGdmI5lO8FHIMsj";
    private static final String id = "LTAI5tFg34gxhYnD3W1XnHFG";
    private static final String secret = "xaAifNXC6gESmF9ruf9YooxIb1ASDE";

    public static String speechToText(String filePath) {
        long millis = System.currentTimeMillis();
        // 更多关于Appkey信息，请参见：https://help.aliyun.com/document_detail/72214.html
        // 获取token
        AccessToken accessToken = new AccessToken(id, secret);
        try {
            accessToken.apply();
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        String token = accessToken.getToken();
        SpeechFlashRecognizerDemo demo = new SpeechFlashRecognizerDemo(appKey);
        int sampleRate = 16000;
        String process = demo.process(filePath, token, "mp3", sampleRate);
        long timeMillis = System.currentTimeMillis();
        log.info("录音文件转文字所花费时间======time={}ms======转成的结果为:::::{}", (timeMillis - millis), process);
        return process;
    }

    public static void main(String[] args) {
        System.out.println(speechToText("D:\\leaderVideoFile\\d4dfc77a3c4b3a970b8037b7986e3a1e_1813527497443127297__uid_s_504__uid_e_audio.mp3"));
    }
}
